﻿using DeviceControl;
using LuaConsole;
using ServoDrivesLib.Controls;
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace 雷赛板卡上位机.Utilities
{
    public static class Sensor
    {
        public static LuaMaster Lua = App.GetService<LuaMaster>();
        public static float GetPos(string port, int baud)
        {

            string buffer = default;
            var serial = Lua["SerialPort"] as SerialPort;
            if (serial == null) Lua["SerialPort"] = new SerialPort();
            serial = Lua["SerialPort"] as SerialPort;
            serial.PortName = port;
            serial.BaudRate = baud;
            serial.ReadTimeout = 1000;
            serial.WriteTimeout = 1000;
            if (!serial.IsOpen) serial.Close();
            serial.Open();
            byte[] data = { 0x4D, 0x30, 0x0D, 0x0A };
            serial.Write(data, 0, data.Length);
            try
            {
                buffer = serial.ReadTo("\r");
            }
            catch (TimeoutException e)
            {
                serial.Close();
                return -999;
            }
            finally
            {
                serial.Close();
            }
            var array = buffer.Split(",");
            if(float.TryParse(array[1],out float res))
            {
                return res;
            }
            else
            {
                return -9999.0f;
            }
        }

        public static float GetForce(string port, int baud, byte id)
        {
            float ret = -999;
            var force = Lua["Force"] as ForceTransducers;
            if (force == null) { Lua["Force"] = new ForceTransducers(port, baud, 8); }
            force = Lua["Force"] as ForceTransducers;
            force.ID = id;
            try
            {
                ret = force.ReadForceValue();
            }
            finally
            {
            }

            return ret;
        }

        public static double GetEncoder(int axis, double unit)
        {
            double pos = double.NaN;
            var card = App.GetService<CardControl>();
            card.SetEquiv(axis, unit);

            card.GetEncoderUnit(axis, ref pos);
            return pos;
        }

        public static void SetEncoderZero(int axis, double unit)
        {
            double pos = double.NaN;
            var card = App.GetService<CardControl>();
            card.SetEquiv(axis, unit);
            card.ClearPosition(axis);
        }

        public static void SetNoOffPower(string port, int baud, bool en)
        {

            var power = Lua["Power"] as PowerSCPI;
            if (power == null) { Lua["Power"] = new PowerSCPI(port, baud); }
            power = Lua["Power"] as PowerSCPI;
            try
            {
                if (en)
                {
                    power?.SetNO();
                }
                else
                {
                    power?.SetOFF();
                }
            }
            finally
            {
                power?.Close();
            }
        }
    }
}
